package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blog.dao.mapper.CommentMapper;
import com.example.blog.dao.mapper.UserMapper;
import com.example.blog.dao.pojo.Comment;
import com.example.blog.dao.pojo.User;
import com.example.blog.service.CommentService;
import com.example.blog.service.LoginService;
import com.example.blog.service.UserService;
import com.example.blog.utils.UserThreadLocal;
import com.example.blog.vo.CommentReply;
import com.example.blog.vo.CommentVo;
import com.example.blog.vo.Result;
import com.example.blog.vo.UserVo;
import com.example.blog.vo.params.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginService loginService;

    @Override
    public Result commentByArticleId(Long id) {
        /**
         * 1、根据文章id查询评论列表 从comment表中查询
         * 2、根据作者id 查询作者的信息
         * 3、判断 level=1时有没有子评论
         * 4、根据评论id进行查询（parentId）
         */
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId,id);
        queryWrapper.eq(Comment::getLevel,1);
        queryWrapper.orderByDesc(Comment::getTime);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVoList = copyList(comments);
//        List<CommentReply> commentReplyList = copyList(comments);
        return Result.success(commentVoList);
    }

    @Override
    public Result comment(String token,CommentParam commentParam) {
        if (token.equals("Bearer null")) {
            return Result.fail(-999, "登录时间超时");
        }
        Long userId = loginService.checkToken(token).getId();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setUserID(userId);
        comment.setComment(commentParam.getComment());
        Date date = new Date();
        comment.setTime(date);
        Long parentId = commentParam.getParentId();
        if(parentId == null || parentId ==0){
            comment.setLevel(1);
        }else{
            comment.setLevel(2);
        }
        comment.setParentId(parentId==null?0:parentId);
        Long toId = commentParam.getToId();
        comment.setToId(toId==null?0:toId);
        this.commentMapper.insert(comment);
        return Result.success(true);


    }

    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        //作者信息
        Long userID = comment.getUserID();
        UserVo userVo = this.userService.findUserVoById(userID);
        commentVo.setName(userVo.getNickname());
        commentVo.setUserId(userVo.getId());
        commentVo.setHeadImg(userVo.getAvatar());
        //子评论
        Integer level = comment.getLevel();
        if(1 == level){
            Long id = comment.getId();
            List<CommentReply> commentsByParentId = findCommentsByParentId(id);;
            System.out.println(commentsByParentId);
            commentVo.setReply(commentsByParentId);
        }
        return commentVo;
    }

    private List<CommentReply> findCommentsByParentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        List<CommentReply> commentReplyList = new ArrayList<>();
        for (Comment comment : comments) {
            CommentReply commentReply = new CommentReply();
            Long userId = comment.getUserID();
            User user = userMapper.selectById(userId);


            commentReply.setId(comment.getId());
            commentReply.setFormId(comment.getUserID());
            commentReply.setFormHeadImg(user.getAvatar());

            Long parentId = comment.getParentId();
            User parent = userMapper.selectById(parentId);

            commentReply.setTo(parent.getNickname());
            commentReply.setToId(parentId);
            comment.setTime(comment.getTime());
            commentReplyList.add(commentReply);
        }
        return commentReplyList;
    }
}
